<template>
  <div class="search-container">
    <!-- 搜索栏》事件监听 -->
    <form action="/">
      <van-search
        v-model="searchText"
        show-action
        background="#3296fa"
        placeholder="请输入搜索关键词"
        @search="onSearch"
        @cancel="onCancel"
        @focus="isResultShow = false"
      />
    </form>

    <!-- 搜索结果 -->
    <search-history v-if="isResultShow" :searchText="searchText" />
    <!-- /搜索历史记录 -->

    <!-- 联想建议 -->
    <!-- 获取并监听输入框内容的变化 -->
    <search-suggestion
      v-else-if="searchText"
      :searchText="searchText"
      @search="onSearch"
    />
    <!-- /联想建议 -->

    <!-- 历史记录 -->
    <search-result v-else />
    <!-- /历史记录 -->
  </div>
</template>
<script>
import SearchHistory from './components/search-history'
import SearchSuggestion from './components/search-suggestion'
import SearchResult from './components/search-result'

export default {
  name: 'search-container',
  data () {
    return {
      isResultShow: false,
      searchText: ''
    }
  },
  components: {
    SearchHistory,
    SearchSuggestion,
    SearchResult
  },
  created () {},
  computed: {},
  methods: {
    onSearch (val) {
      //   this.$taost(val)
      //   console.log(val)
      this.searchText = val
      this.isResultShow = true
    },
    onCancel () {
      //   this.$taost('取消')
      console.log('onCancel')
    }
  }
}
</script>
<style lang="less" scoped></style>
